// pages/sendlove/sendlove.js
const db = wx.cloud.database(); //初始化数据库
import {
  getTimeNow
} from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    a:"",
    imgs: [],
    Location: '所在位置',
    address: '',
    latitude: '',
    longitude: '',
    videos: [],
    types: [],
  },


  // 上传图片
  upimgs() {
    let that = this
    wx.showActionSheet({
      itemList: ['上传图片', '上传视频'],
      success(res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          wx.chooseImage({
            count: 9,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
              wx.showLoading({
                title: '上传中...',
              })
              let time = Date.now()
              for (var i = 0; i <= res.tempFilePaths.length; i++) {
                wx.cloud.uploadFile({
                    cloudPath: "loveimages/" + time + i,
                    filePath: res.tempFilePaths[i]
                  })
                  .then(res => {
                    that.setData({
                      imgs: that.data.imgs.concat(res.fileID)
                    })
                    wx.hideLoading()
                    wx.showToast({
                      icon: "none",
                      title: '上传成功',
                    })
                  })
              }
            }
          })
        }else if(res.tapIndex == 1) {
          wx.chooseMedia({
            count: 1,
            mediaType: ['video'],
            sourceType: ['album', 'camera'],
            maxDuration: 30,
            camera: 'back',
            success(res) {
              console.log('xxx',res.tempFiles)
              that.setData({
                types: res.tempFiles ,   
                //types: that.data.types.concat(res.tempFiles)  
            })
                wx.showLoading({
                  title: '上传中...',
                })
              let time = Date.now()
              for (var i = 0; i <= res.tempFiles.length; i++) {
                console.log('yyy',  res.tempFiles[i] )
                console.log('zzz',  res.tempFiles[i].tempFilePath )
                wx.cloud.uploadFile({
                    cloudPath: "loveimages/" + time + i,
                    filePath: res.tempFiles[i].tempFilePath
                  })
                   .then(res => {
                    that.setData({
                     // videos: that.data.videos.concat(res.fileID)
                      videos:  res.fileID
                    }) 
                    wx.hideLoading()
                    wx.showToast({
                      icon: "none",
                      title: '上传成功',
                    })
                    }) 
              }
            }
          })
        }
      }
    })
  },

  // 点击预览图片
  look(e) {
    wx.previewImage({
      urls: this.data.imgs,
      current: e.currentTarget.dataset.src,
    })
  },

  // 点击发布 提交数据
  submitform(e) {
    const that = this
    console.log(e.detail.value.content)
    if (!wx.getStorageSync('key')) {
      wx.showModal({
        title: '提示',
        content: "你暂未登录,请先去登录",
        showCancel: false,
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '../my/my',
            })
          }
        }
      })
    } else {
      if (e.detail.value.content == "") {
        wx.showToast({
          title: '输入内容不能为空',
          icon: "none"
        })
      } else {
        wx.showModal({
          title: '发表',
          content: "发表后对所有人可见噢",
          success(res) {
            if (res.confirm) {
              db.collection("love").add({
                  data: {
                    lovesrc: that.data.imgs,
                    lovevideos: that.data.videos ,
                    address: that.data.address,
                    latitude: that.data.latitude,
                    longitude: that.data.longitude,
                    content: e.detail.value.content,
                    createTime: db.serverDate(),
                    sendtime: getTimeNow(),
                    sayavatarUrl: wx.getStorageSync('key').avatarUrl,
                    saynickName: wx.getStorageSync('key').nickName
                  }
                })
                .then(res => {
                  wx.showToast({
                    title: '发布成功',
                    icon: "none",
                    success() {
/*                       setTimeout(() => {  
                      wx.switchTab({
                        url: '../love/love',
                      })
                    }, 500) */


                      setTimeout(() => {  
/*                         wx.navigateTo ({
                          url: '../love/love',
                        }) */
                        wx.reLaunch({
                          url: '../love/love',
                        })
                      }, 500)
                    }
                  })
                })
            }
          }
        })
      }
    }
  },

  // 用户选择 位置
  chooseAddress(){
    const that = this
    wx.showActionSheet({
      itemList: ['选择位置','不显示位置'],
      success(res) {
        if (res.tapIndex == 0) {
        wx.chooseLocation({
          success: (res) => {
            console.log('当前位置为',res)
            that.setData({
              latitude: res.latitude,
              longitude: res.longitude,
              address: res.name + ' - ' + res.address,
              Location: res.name + ' - ' + res.address,
            })
          }          
        })
      }else{
        that.setData({
          Location: "不显示位置",
          latitude: '',
          longitude: '',
          address:  '',
       
        })
        console.log("用户不显示位置")
        return
      }
    }

  })


  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     // 过审核
     const that = this
     db.collection("a").get()
       .then(res => {
           console.log("11111111",res.data[0].a)
         that.setData({
           a: res.data[0].a,
         })
       })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})